<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>点名器</title>
    <style>
      .box {
        height: 50px;
        width: 100px;
        border: 2px solid #000;
        color: #000;
        text-align: center;
        line-height: 50px;
        font-size: 20px;
      }
      .btn {
        height: 50px;
        width: 100px;
        border: 2px solid #000;
        color: black;
        text-align: center;
        line-height: 50px;
      }

      .btn:hover {
        cursor: pointer;
        background-color: aliceblue;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
    <div class="btn">开始</div>
    <script>
      let boxEle = document.querySelector('.box')
      let btnEle = document.querySelector('.btn')
      let timer

      btnEle.addEventListener('click', function () {
        if (btnEle.textContent === '开始') {
            if (timer) {
                return
            }
            timer = setInterval(function () {
            let arr = ['1', '2', '3', '4', '5']
            boxEle.innerText = arr[parseInt(Math.random() * arr.length)]
          }, 200)
          btnEle.innerText = '停止'
        } else {
          clearInterval(timer)
          btnEle.innerText = '开始'
          timer = undefined
        }
      })
    </script>
  </body>
</html>
